package chucnang;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.border.TitledBorder;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.table.DefaultTableModel;

import check_form.CheckString;

import com.toedter.calendar.JDateChooser;

import form.DSJ_DieuKhoanThanhToan;
import form.DSJ_FormMuaHang;
import form.DSJ_KhachHang;
import form.DSJ_NhanVien;
import form.DSJ_XuatHang;
import form.FormBanHang;
import form.FormUpdateBanHang;

import trangchu.TrangChu;
import trangchu.UtilDelegate;
import javax.swing.JCheckBox;

public class BanHang extends JPanel implements ActionListener {
	
	private JPanel plNorth,plCenter,plSouth;
	public JComboBox cmbTenKH;
	public JTextField txtDiaChi;
	public JComboBox cmbMa;
	public JTextField txtDienThoai;
	public JComboBox cmbTenNV;
	public JComboBox cmbThanhToan;
	public JTextField txtPhanTram,txtChietKhau,txtTong,txtDaTra,txtConLai;
	public JButton btnThem,btnSua,btnXoa,btnTaoMoi;
	public  JTable table;
	public DefaultTableModel model;
	public JDateChooser dateStart;
	public JDateChooser datehanthanhtoan;
	
	private MenuAction_MuaHang pMenu;
	public TitledBorder title;
	String[][] data = {};
    String[] colunms = {"Mã Hàng","Tên Hàng","Kho Hàng","Đơn Vị","Đơn Giá","Số Lượng","Thành Tiền","ID"};
    ResultSet rs;
	PreparedStatement st;
	Connection con =UtilDelegate.getConnection();
	private FormUpdateBanHang updatebanhang;
	public JLabel lblGhiChu;
	public JTextField txtGhiChu;
	public JLabel lblPhieu;
	public JTextField txtPhieu;
	private BanHang frame;
	private JCheckBox cbXacNhanKH;
	private JLabel lblsao;
	
	public BanHang(TrangChu trangchu) {
    	    	
    	this.setLayout(new BorderLayout(15,15));
    	plNorth = new JPanel();
		add(plNorth,BorderLayout.NORTH);
		GridBagLayout gbl_plNorth = new GridBagLayout();
		gbl_plNorth.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_plNorth.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
		gbl_plNorth.columnWeights = new double[]{0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		gbl_plNorth.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		plNorth.setLayout(gbl_plNorth);
		
		title = new TitledBorder("Xuất Hàng");
		plNorth.setBorder(title);
		GridBagConstraints gbc_lblThongTin = new GridBagConstraints();
		gbc_lblThongTin.insets = new Insets(0, 0, 5, 5);
		gbc_lblThongTin.gridx = 0;
		gbc_lblThongTin.gridy = 1;
		//plNorth.add(lblThongTin, gbc_lblThongTin);
		
		JLabel lblTenKH = new JLabel("Tên Khách Hàng: ");
		GridBagConstraints gbc_lblTenKH = new GridBagConstraints();
		gbc_lblTenKH.anchor = GridBagConstraints.WEST;
		gbc_lblTenKH.insets = new Insets(0, 0, 5, 5);
		gbc_lblTenKH.gridx = 1;
		gbc_lblTenKH.gridy = 2;
		plNorth.add(lblTenKH, gbc_lblTenKH);
		
		cmbTenKH = new JComboBox();
		GridBagConstraints gbc_cmbTenKH = new GridBagConstraints();
		gbc_cmbTenKH.fill = GridBagConstraints.HORIZONTAL;
		gbc_cmbTenKH.gridwidth = 18;
		gbc_cmbTenKH.insets = new Insets(0, 0, 5, 5);
		gbc_cmbTenKH.gridx = 2;
		gbc_cmbTenKH.gridy = 2;
		plNorth.add(cmbTenKH, gbc_cmbTenKH);
		
		
		JLabel lblMa = new JLabel("Mã: ");
		GridBagConstraints gbc_lblMa = new GridBagConstraints();
		gbc_lblMa.anchor = GridBagConstraints.WEST;
		gbc_lblMa.insets = new Insets(0, 0, 5, 5);
		gbc_lblMa.gridx = 21;
		gbc_lblMa.gridy = 2;
		plNorth.add(lblMa, gbc_lblMa);
		
		cmbMa = new JComboBox();
		GridBagConstraints gbc_cmbMa = new GridBagConstraints();
		gbc_cmbMa.gridwidth = 1;
		gbc_cmbMa.insets = new Insets(0, 0, 5, 5);
		gbc_cmbMa.fill = GridBagConstraints.HORIZONTAL;
		gbc_cmbMa.gridx = 22;
		gbc_cmbMa.gridy = 2;
		plNorth.add(cmbMa, gbc_cmbMa);
		
		JLabel lblDiaChi = new JLabel("Địa Chỉ: ");
		GridBagConstraints gbc_lblDiaChi = new GridBagConstraints();
		gbc_lblDiaChi.anchor = GridBagConstraints.WEST;
		gbc_lblDiaChi.insets = new Insets(0, 0, 5, 5);
		gbc_lblDiaChi.gridx = 1;
		gbc_lblDiaChi.gridy = 3;
		plNorth.add(lblDiaChi, gbc_lblDiaChi);
		
		txtDiaChi = new JTextField();
		txtDiaChi.setEditable(false);
		GridBagConstraints gbc_txtDiaChi = new GridBagConstraints();
		gbc_txtDiaChi.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtDiaChi.gridwidth = 18;
		gbc_txtDiaChi.insets = new Insets(0, 0, 5, 5);
		gbc_txtDiaChi.gridx = 2;
		gbc_txtDiaChi.gridy = 3;
		plNorth.add(txtDiaChi, gbc_txtDiaChi);
		txtDiaChi.setColumns(10);
		
		JLabel lblDienThoai = new JLabel("Điện Thoại: ");
		GridBagConstraints gbc_lblDienThoai = new GridBagConstraints();
		gbc_lblDienThoai.anchor = GridBagConstraints.WEST;
		gbc_lblDienThoai.insets = new Insets(0, 0, 5, 5);
		gbc_lblDienThoai.gridx = 21;
		gbc_lblDienThoai.gridy = 3;
		plNorth.add(lblDienThoai, gbc_lblDienThoai);
		
		txtDienThoai = new JTextField();
		txtDienThoai.setEditable(false);
		GridBagConstraints gbc_txtDienThoai = new GridBagConstraints();
		gbc_txtDienThoai.gridwidth = 1;
		gbc_txtDienThoai.insets = new Insets(0, 0, 5, 5);
		gbc_txtDienThoai.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtDienThoai.gridx = 22;
		gbc_txtDienThoai.gridy = 3;
		plNorth.add(txtDienThoai, gbc_txtDienThoai);
		txtDienThoai.setColumns(10);
		
		JLabel lblTenNN = new JLabel("Nhân Viên Nhận Hàng: ");
		GridBagConstraints gbc_lblTenNN = new GridBagConstraints();
		gbc_lblTenNN.anchor = GridBagConstraints.WEST;
		gbc_lblTenNN.insets = new Insets(0, 0, 5, 5);
		gbc_lblTenNN.gridx = 1;
		gbc_lblTenNN.gridy = 4;
		plNorth.add(lblTenNN, gbc_lblTenNN);
		
		cmbTenNV = new JComboBox();
		GridBagConstraints gbc_cmbTenNV = new GridBagConstraints();
		gbc_cmbTenNV.gridwidth = 18;
		gbc_cmbTenNV.insets = new Insets(0, 0, 5, 5);
		gbc_cmbTenNV.fill = GridBagConstraints.HORIZONTAL;
		gbc_cmbTenNV.gridx = 2;
		gbc_cmbTenNV.gridy = 4;
		plNorth.add(cmbTenNV, gbc_cmbTenNV);
		
		JLabel lblNgay = new JLabel("Ngày: ");
		GridBagConstraints gbc_lblNgay = new GridBagConstraints();
		gbc_lblNgay.anchor = GridBagConstraints.WEST;
		gbc_lblNgay.insets = new Insets(0, 0, 5, 5);
		gbc_lblNgay.gridx = 21;
		gbc_lblNgay.gridy = 4;
		plNorth.add(lblNgay, gbc_lblNgay);
			
		GridBagConstraints gbc_txtNgay = new GridBagConstraints();
		gbc_txtNgay.gridwidth = 1;
		gbc_txtNgay.insets = new Insets(0, 0, 5, 5);
		gbc_txtNgay.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtNgay.gridx = 22;
		gbc_txtNgay.gridy = 4;
		dateStart = new JDateChooser();
		dateStart.setDateFormatString("dd/MM/yyyy");
		Date todayD=new Date(System.currentTimeMillis());
		dateStart.setDate(todayD);		
		plNorth.add(dateStart,gbc_txtNgay);
		
		cbXacNhanKH = new JCheckBox("Xác nhận khách hàng");
		GridBagConstraints gbc_cbXacNhanKH = new GridBagConstraints();
		gbc_cbXacNhanKH.anchor = GridBagConstraints.EAST;
		gbc_cbXacNhanKH.insets = new Insets(0, 0, 5, 5);
		gbc_cbXacNhanKH.gridx = 23;
		gbc_cbXacNhanKH.gridy = 4;
		plNorth.add(cbXacNhanKH, gbc_cbXacNhanKH);
		
		lblsao = new JLabel("(*)");
		lblsao.setForeground(Color.RED);
		GridBagConstraints gbc_lblsao = new GridBagConstraints();
		gbc_lblsao.anchor = GridBagConstraints.WEST;
		gbc_lblsao.insets = new Insets(0, 0, 5, 5);
		gbc_lblsao.gridx = 24;
		gbc_lblsao.gridy = 4;
		plNorth.add(lblsao, gbc_lblsao);
		
		JLabel lblThanhToan = new JLabel("Điều Khoản Thanh Toán:");
		GridBagConstraints gbc_lblThanhToan = new GridBagConstraints();
		gbc_lblThanhToan.anchor = GridBagConstraints.WEST;
		gbc_lblThanhToan.insets = new Insets(0, 0, 5, 5);
		gbc_lblThanhToan.gridx = 1;
		gbc_lblThanhToan.gridy = 5;
		plNorth.add(lblThanhToan, gbc_lblThanhToan);
		
		cmbThanhToan = new JComboBox();	
		GridBagConstraints gbc_cmbThanhToan = new GridBagConstraints();
		gbc_cmbThanhToan.gridwidth = 18;
		gbc_cmbThanhToan.insets = new Insets(0, 0, 5, 5);
		gbc_cmbThanhToan.fill = GridBagConstraints.HORIZONTAL;
		gbc_cmbThanhToan.gridx = 2;
		gbc_cmbThanhToan.gridy = 5;
		plNorth.add(cmbThanhToan, gbc_cmbThanhToan);
		
		JLabel lblHanThanhToan = new JLabel("Hạn Thanh Toán:");
		GridBagConstraints gbc_lblHanThanhToan = new GridBagConstraints();
		gbc_lblHanThanhToan.anchor = GridBagConstraints.WEST;
		gbc_lblHanThanhToan.insets = new Insets(0, 0, 5, 5);
		gbc_lblHanThanhToan.gridx = 21;
		gbc_lblHanThanhToan.gridy = 5;
		plNorth.add(lblHanThanhToan, gbc_lblHanThanhToan);
		
		GridBagConstraints gbc_txtHanThanhToan = new GridBagConstraints();
		gbc_txtHanThanhToan.gridwidth = 1;
		gbc_txtHanThanhToan.insets = new Insets(0, 0, 5, 5);
		gbc_txtHanThanhToan.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtHanThanhToan.gridx = 22;
		gbc_txtHanThanhToan.gridy = 5;
		datehanthanhtoan = new JDateChooser();
		datehanthanhtoan.setDateFormatString("dd/MM/yyyy");
		plNorth.add(datehanthanhtoan,gbc_txtHanThanhToan);
		
		lblGhiChu = new JLabel("Ghi Chú:");
		GridBagConstraints gbc_lblGhiChu = new GridBagConstraints();
		gbc_lblGhiChu.anchor = GridBagConstraints.WEST;
		gbc_lblGhiChu.insets = new Insets(0, 0, 0, 5);
		gbc_lblGhiChu.gridx = 1;
		gbc_lblGhiChu.gridy = 6;
		plNorth.add(lblGhiChu, gbc_lblGhiChu);
		
		txtGhiChu = new JTextField();
		GridBagConstraints gbc_txtGhiChu = new GridBagConstraints();
		gbc_txtGhiChu.gridwidth = 18;
		gbc_txtGhiChu.insets = new Insets(0, 0, 0, 5);
		gbc_txtGhiChu.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtGhiChu.gridx = 2;
		gbc_txtGhiChu.gridy = 6;
		plNorth.add(txtGhiChu, gbc_txtGhiChu);
		txtGhiChu.setColumns(10);
		
		lblPhieu = new JLabel("Phiếu:");
		GridBagConstraints gbc_lblPhieu = new GridBagConstraints();
		gbc_lblPhieu.anchor = GridBagConstraints.WEST;
		gbc_lblPhieu.insets = new Insets(0, 0, 0, 5);
		gbc_lblPhieu.gridx = 21;
		gbc_lblPhieu.gridy = 6;
		plNorth.add(lblPhieu, gbc_lblPhieu);
		
		txtPhieu = new JTextField();
		GridBagConstraints gbc_txtPhieu = new GridBagConstraints();
		gbc_txtPhieu.insets = new Insets(0, 0, 0, 5);
		gbc_txtPhieu.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtPhieu.gridx = 22;
		gbc_txtPhieu.gridy = 6;
		plNorth.add(txtPhieu, gbc_txtPhieu);
		txtPhieu.setColumns(11);
		txtPhieu.setEditable(false);
		
    	
		
		plCenter = new JPanel();		
		add(plCenter,BorderLayout.CENTER);
		plCenter.setLayout(new BorderLayout());
    	JScrollPane scrollPane = new JScrollPane();
    	pMenu = new MenuAction_MuaHang(trangchu);
    	scrollPane.setComponentPopupMenu(pMenu);
    	plCenter.add(scrollPane);
		
    	model = new DefaultTableModel(data, colunms);
    	table = new JTable(model);
//    	table.addPropertyChangeListener(new PropertyChangeListener() {
//    		public void propertyChange(PropertyChangeEvent arg0) {
//    			getInfo();
//    		}
//    	});
    	table.getColumnModel().getColumn(1).setPreferredWidth(250);
		table.getColumnModel().getColumn(2).setPreferredWidth(150);
		table.getColumnModel().getColumn(7).setMaxWidth(0);
		table.getColumnModel().getColumn(7).setMinWidth(0);
		table.getColumnModel().getColumn(7).setPreferredWidth(0);
    	scrollPane.setViewportView(table);
		
		plSouth = new JPanel();
		plSouth.setLayout(new BorderLayout());
		add(plSouth,BorderLayout.SOUTH);
		JPanel plSouth1 = new JPanel();
		plSouth1.setLayout(new BorderLayout());
		plSouth.add(plSouth1,BorderLayout.CENTER);
		JPanel plbuttom = new JPanel();
		plbuttom.setLayout(new FlowLayout(FlowLayout.LEFT));
		plSouth1.add(plbuttom,BorderLayout.WEST);		
		btnThem = new JButton("Thêm",new ImageIcon("images/them1.gif"));	
		plbuttom.add(btnThem);
		btnSua = new JButton("Sửa",new ImageIcon("images/sua1.gif"));	
		plbuttom.add(btnSua);
		btnXoa = new JButton("Xóa",new ImageIcon("images/xoa1.gif"));	
		plbuttom.add(btnXoa);
//		btnXem = new JButton("Xem");
//		plbuttom.add(btnXem);
		btnTaoMoi = new JButton("Tạo Mới",new ImageIcon("images/new1.gif"));
		plbuttom.add(btnTaoMoi);
		
		JPanel pltxtTong = new JPanel();
		plbuttom.setLayout(new FlowLayout(FlowLayout.RIGHT));
		plSouth1.add(pltxtTong,BorderLayout.EAST);
		
		JLabel lblChietKhau = new JLabel("Chiết Khấu: ");
		pltxtTong.add(lblChietKhau);
		txtChietKhau = new JTextField(5);
		txtChietKhau.setText("0");
		txtChietKhau.addCaretListener(new CaretListener() {
		public void caretUpdate(CaretEvent e) {
			if(CheckString.OnlyNumber(txtChietKhau.getText(), 5, 1)==true){
				getInfoChietKhau();
				txtChietKhau.setBackground(Color.white);
			}else{
				JOptionPane.showMessageDialog(txtDaTra, "Lỗi nhập!", "Lỗi", JOptionPane.OK_OPTION);
				txtChietKhau.setBackground(Color.red);
			}
		}
		});
		pltxtTong.add(txtChietKhau);
		JLabel lblPhanTram = new JLabel("(%) ");
		lblPhanTram.setForeground(Color.red);
		pltxtTong.add(lblPhanTram);
		
		JLabel lblTong = new JLabel("Thành Tiền: ");
		pltxtTong.add(lblTong);
		txtTong = new JTextField(10);
		txtTong.setText("0");
		pltxtTong.add(txtTong);
		txtTong.setEditable(false);
		
		JLabel lblDaTra = new JLabel("--- Đã Trả: ");
		pltxtTong.add(lblDaTra);
		txtDaTra = new JTextField(10);
		txtDaTra.setText("0");
		txtDaTra.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent e) {
				txtDaTra.setBackground(Color.white);
				if(CheckString.OnlyNumber(txtDaTra.getText(), 9, 1)==true){
					getInfoConLai();
					
				}else if(CheckString.OnlyNumber(txtDaTra.getText(), 9, 1)==false){
					JOptionPane.showMessageDialog(txtDaTra, "Lỗi nhập!", "Lỗi", JOptionPane.OK_OPTION);
					txtDaTra.setBackground(Color.red);
				}
			}
		});
		pltxtTong.add(txtDaTra);
		
		txtDaTra.setEditable(false);
		
		
		JLabel lblConLai = new JLabel("==> Còn Lại: ");
		pltxtTong.add(lblConLai);
		txtConLai = new JTextField(10);
		pltxtTong.add(txtConLai);
		txtConLai.setEditable(false);
		txtConLai.setText("0");
		
		JPanel plSouth2 = new JPanel();
		plSouth.add(plSouth2,BorderLayout.SOUTH);		
		plSouth2.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		JLabel lblgoithieu = new JLabel("Phần mềm quản lý bán hàng ShopM                ");
		lblgoithieu.setFont(new Font("Tahoma", 0, 10));
		lblgoithieu.setFont(new Font("Tahoma", 3, 11));		
		lblgoithieu.setForeground(Color.blue);
		plSouth2.add(lblgoithieu);
		final JLabel lblgioserver = new JLabel();
		plSouth2.add(lblgioserver);
		 final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a | dd/MM/yyyy");
	        Timer time = new Timer(1000, new ActionListener()
	        {
	            public void actionPerformed(ActionEvent e)
	            {
	                Date d = new Date(System.currentTimeMillis());
	                lblgioserver.setText(sdf.format(d.getTime()));
	            }
	        });
	        time.start();
			
	        //dang ki su kien
	        btnThem.addActionListener(this);
	        btnSua.addActionListener(this);
	        btnXoa.addActionListener(this);
	        btnTaoMoi.addActionListener(this);
	        cmbMa.addActionListener(this);
	        cmbTenKH.addActionListener(this);
	        cmbThanhToan.addActionListener(this);
	        cbXacNhanKH.addActionListener(this);
	      //enabled buttom     
			
	        int i = model.getRowCount();
			if(i==0){
				btnSua.setEnabled(false);
		        btnXoa.setEnabled(false);
		        btnTaoMoi.setEnabled(false);
			}
	        
	        
	        DbselectKH();
	        DbselectNhanVien();
	        DbselectDieuKhoan();
	        DBphieu();	       
	        
    }
	//hiển thị thong tin khach hang
	
		public void DbselectKH(){
			DSJ_KhachHang data = new DSJ_KhachHang();
			Vector<DSJ_KhachHang> getKhachHang = data.getUsers();
			for(int i=0;i<getKhachHang.size();i++){
				cmbMa.addItem(getKhachHang.get(i).getMaKH());
				cmbTenKH.addItem(getKhachHang.get(i).getHoTenKH());
				txtDiaChi.setText(getKhachHang.get(0).getDiaChi());
				txtDienThoai.setText(getKhachHang.get(0).getDienThoai());
			}
		}
		//hiển thị tên nhân viên
		public void DbselectNhanVien(){
			DSJ_NhanVien data = new DSJ_NhanVien();
			Vector<DSJ_NhanVien> getnhanvien = data.getUsers();
			for(int i=0;i<getnhanvien.size();i++){
				cmbTenNV.addItem(getnhanvien.get(i).getHoTenNV());
			}
		}
		
		//hiển thi điều khoản thanh toan
		public void DbselectDieuKhoan(){
			DSJ_DieuKhoanThanhToan data = new DSJ_DieuKhoanThanhToan();
			Vector<DSJ_DieuKhoanThanhToan> getDieuKhoan = data.getDieuKhoan();
			for(int i=0;i<getDieuKhoan.size();i++){
				cmbThanhToan.addItem(getDieuKhoan.get(i).getTenDK());
			}
		}
		public void getInfo(){
			Float a =(float) 0;
			for (int i = 0; i < table.getModel().getRowCount(); i++) {
				a = a +Float.parseFloat((String) table.getModel().getValueAt(i, 6));
			}
			String c = txtChietKhau.getText();
			Float n2 = Float.parseFloat(c);
			Float a1 = a-((n2/100)*a);
			String d = String.valueOf(a1); 
			txtTong.setText(d);	
		}
		//Con lai
		public void getInfoConLai(){			
			Float a =(float) 0;
			for (int i = 0; i < table.getModel().getRowCount(); i++) {
				a = a +Float.parseFloat((String) table.getModel().getValueAt(i, 6));
			}
			String b = txtChietKhau.getText();
			Float n1 = Float.parseFloat(b);
			String c = txtDaTra.getText();
			Float n2 = Float.parseFloat(c);
			Float a1 = (a-((n1/100)*a)-n2);
			//Float a1 = a-n2;
			String d = String.valueOf(a1);
			txtConLai.setText(d);
		}
		//Chietkhau
		public void getInfoChietKhau(){
			Float a =(float) 0;
			for (int i = 0; i < table.getModel().getRowCount(); i++) {
				a = a +Float.parseFloat((String) table.getModel().getValueAt(i, 6));
			}
			String b = txtDaTra.getText();
			Float n1 = Float.parseFloat(b);
			
			String c = txtChietKhau.getText();
			Float n2 = Float.parseFloat(c);
			Float a1 = a-((n2/100)*a);
			Float a2 = (a-((n2/100)*a)-n1);
			String d = String.valueOf(a1); 
			String d1 = String.valueOf(a2);
			txtTong.setText(d);
			txtConLai.setText(d1);
		}
		//getin
		public void bindDataToForm(FormUpdateBanHang updatebanhang){
			int row = this.table.getSelectedRow();
			updatebanhang.cmbMaHang.setSelectedItem(this.table.getModel().getValueAt(row, 0).toString());
			updatebanhang.cmbTenHang.setSelectedItem(this.table.getModel().getValueAt(row, 1).toString());
			updatebanhang.txtKhoHang.setText(this.table.getModel().getValueAt(row, 2).toString());
			updatebanhang.txtDonVi.setText(this.table.getModel().getValueAt(row, 3).toString());
			updatebanhang.txtDonGia.setText(this.table.getModel().getValueAt(row, 4).toString());
			updatebanhang.txtSoLuong.setText(this.table.getModel().getValueAt(row, 5).toString());
			updatebanhang.txtThanhTien.setText(this.table.getModel().getValueAt(row, 6).toString());

		}
		//hiển thị phieu

		public void DBphieu(){
			//tang so luong
			DSJ_XuatHang data = new DSJ_XuatHang();
			String getMuaHangs = data.selectmaXH(data);
			if(getMuaHangs==null){
				txtPhieu.setText("BH100001");
			}
			else{
				Integer n1 = Integer.parseInt(getMuaHangs);
				Integer n2 = 100001;
				int t = n1+n2;
				String b = String.valueOf(t); 
				txtPhieu.setText("BH"+b);
			}
		}
		public void deleteAllRows( DefaultTableModel model ){
			for( int i = model.getRowCount() - 1; i >= 0; i-- )
			{
				model.removeRow(i);
			}
		}
		
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == cbXacNhanKH){
			int xoa = JOptionPane.showConfirmDialog(cbXacNhanKH,  "Bạn đã chắc chọn đúng khách hàng cần giao dịch?\nKích vào nút Tạo Mới để kết thúc giao dịch này.", "", JOptionPane.YES_NO_OPTION);
			if(xoa == JOptionPane.YES_OPTION){
				cmbTenKH.setEnabled(false);
				cmbMa.setEnabled(false);
				cbXacNhanKH.setEnabled(false);
				DBphieu();
			}else{
				cbXacNhanKH.setSelected(false);
			}
			
		}
		if(e.getSource()==cmbMa){
			try {
			       String sql = "select * from KhachHang WHERE MaKH = '" +cmbMa.getSelectedItem().toString() + "'";
			        st = con.prepareStatement(sql);
					ResultSet rs = st.executeQuery();
			        while(rs.next()){
			        	cmbTenKH.setSelectedItem(rs.getString("HoTenKH"));
			        	txtDiaChi.setText(rs.getString("DiaChi"));
			        	txtDienThoai.setText(rs.getString("DienThoai"));
			        }
			                       
			    } catch (SQLException ex) {
			       ex.printStackTrace();
			    }
			
		}
		if(e.getSource()==cmbTenKH){
			try {
			       String sql = "select * from KhachHang WHERE HoTenKH = N'" +cmbTenKH.getSelectedItem().toString() + "'";
			        st = con.prepareStatement(sql);
					ResultSet rs = st.executeQuery();
			        while(rs.next()){
			        	cmbMa.setSelectedItem(rs.getString("MaKH"));
			        	txtDiaChi.setText(rs.getString("DiaChi"));
			        	txtDienThoai.setText(rs.getString("DienThoai"));
			        }
			                       
			    } catch (SQLException ex) {
			       ex.printStackTrace();
			    }
			
		}
		if(e.getSource()==cmbThanhToan){
			if(cmbThanhToan.getSelectedItem().toString().equals("Thanh Toán Ngay") ){
				txtDaTra.setEditable(false);
				Date todayD=new Date(System.currentTimeMillis());
				datehanthanhtoan.setDate(todayD);
				//txtDaTra.setText(txtTong.getText().toString());
				//txtConLai.setText("0");
				datehanthanhtoan.setEnabled(false);
			}
			if(cmbThanhToan.getSelectedItem().toString().equals("Công Nợ")){
				//txtDaTra.setText("0");
				txtDaTra.setEditable(true);
				datehanthanhtoan.setEnabled(true);				
			}
		}
		
		if(e.getSource() == btnThem){
			
			if(cbXacNhanKH.isSelected()==false){
				JOptionPane.showMessageDialog(btnThem, "Vui lòng xác nhận khách hàng cần giao dịch.", "Lỗi Xác Nhận", JOptionPane.OK_OPTION);		
			}else{
				new FormBanHang(this).setVisible(true);
			}
			
		}
		if(e.getSource() == btnSua){
			updatebanhang =new FormUpdateBanHang(this);
			bindDataToForm(updatebanhang);
			updatebanhang.setVisible(true);
		}
		if(e.getSource() == btnXoa){
			int xoa = JOptionPane.showConfirmDialog(btnXoa,  "Bạn có chắc muốn xóa không?", "Delete", JOptionPane.YES_NO_OPTION);	
			if(xoa == JOptionPane.YES_OPTION){
				
				int row = this.table.getSelectedRow();
				DSJ_FormMuaHang data = new DSJ_FormMuaHang();
				FormBanHang frmbanhang = new FormBanHang(frame);
				data.setMaSP(frmbanhang.cmbMaHang.getSelectedItem().toString());
				String getMuaHangs = data.selecthanghoa(data);
				
				Integer n1 = Integer.parseInt(getMuaHangs);
				String s= this.table.getModel().getValueAt(row, 5).toString();
				Integer n3 = Integer.parseInt(s);
				int tong = n1+n3;
				String d = String.valueOf(tong); 
				
				data.setSoLuong(d);
				data.updatehanghoa(data);
				
				String id = this.table.getModel().getValueAt(row, 7).toString();
				DSJ_XuatHang b = new DSJ_XuatHang();
				//b.setNhapHang_ID(id);
				b.del(id);
				model.removeRow(row);
				
				getInfoConLai();
				 int i = model.getRowCount();
					if(i==0){
						btnSua.setEnabled(false);
				        btnXoa.setEnabled(false);
				        btnTaoMoi.setEnabled(false);
				        
				        cbXacNhanKH.setEnabled(true);
						cbXacNhanKH.setSelected(false);
						cmbTenKH.setEnabled(true);
						cmbMa.setEnabled(true);
						
				        txtTong.setText("0");
				        txtDaTra.setText("0");
				        txtConLai.setText("0");
				        txtChietKhau.setText("0");
				        
					}
				
			}
			
		}
		if(e.getSource() == btnTaoMoi){
			String soluong = txtTong.getText();
			Float intsoluong = Float.parseFloat(soluong);
			String datra = txtDaTra.getText();
			Float intdatra = Float.parseFloat(datra);
			if(intsoluong<intdatra){
				JOptionPane.showMessageDialog(btnTaoMoi, "Vui lòng nhập lại!", "Lỗi Đã Trả", JOptionPane.OK_OPTION);
				txtDaTra.setBackground(Color.red);
			}else{
				deleteAllRows(model);		
				btnSua.setEnabled(false);
		        btnXoa.setEnabled(false);
		        btnTaoMoi.setEnabled(false);
		        
		        DSJ_XuatHang delegate = new DSJ_XuatHang();
		        DSJ_XuatHang xuathang = new DSJ_XuatHang();
		        DSJ_KhachHang khachhang = new DSJ_KhachHang();
		        
		        khachhang.setMaKH(cmbMa.getSelectedItem().toString());
				khachhang.setHoTenKH(cmbTenKH.getSelectedItem().toString());
				java.util.Date datengay =  dateStart.getDate();
				String datengaynhaphang = String.format("%1$td-%1$tm-%1$tY", datengay);
				xuathang.setNgay(datengaynhaphang);
				xuathang.setChungTu(txtPhieu.getText());
				xuathang.setGhiChu(txtGhiChu.getText());
				xuathang.setTongThanhToan(txtTong.getText().toString());
				xuathang.setDaTra(txtDaTra.getText().toString());
				xuathang.setConLai(txtConLai.getText().toString());
				xuathang.setLoaiChungTu(title.getTitle().toString());
				
				delegate.insertGiaoDichBanHang(xuathang, khachhang);

				cbXacNhanKH.setEnabled(true);
				cbXacNhanKH.setSelected(false);
				cmbTenKH.setEnabled(true);
				cmbMa.setEnabled(true);
				
				txtDaTra.setBackground(Color.white);
				txtTong.setText("0");
		        txtDaTra.setText("0");
		        txtConLai.setText("0");
		        txtChietKhau.setText("0");
			}
			
			
		}
		
	}

}
